package com.mobbanana.gameHelper;

import android.content.Context;
import android.content.pm.ApplicationInfo;

import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * @Description
 * @Author imchen
 * @CreateTime 2019/8/1 14:17
 */
public class FileUtil {

    private static final boolean isXOR = false;
    private static final String CHANNEL_PREFIX = "META-INF/cztchannel_";

    public static String readAssetsFile(Context context, String filePath) {
        InputStream ips = null;
        ByteArrayOutputStream bos = null;
        String content = null;
        try {
            ips = context.getAssets().open(filePath);
            bos = new ByteArrayOutputStream();
            StringBuilder builder = new StringBuilder();
            byte[] buff = new byte[1024];
            int length = 0;
            while ((length = ips.read(buff)) != -1) {
                ips.read(buff, 0, length);
                bos.write(buff, 0, length);
            }
            byte[] bytes = bos.toByteArray();
            if (isXOR) {
                bytes = Decrypt(bytes);
            }
            content = new String(bytes, "UTF-8");
        } catch (IOException e) {
            LogUtil.e(e);
        } finally {
            try {
                if (ips != null) {
                    ips.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (Exception e) {
                LogUtil.e(e);
            }

        }

        return content;
    }

    public static JSONObject readAssetsFileToJSON(Context context, String filePath) {
        JSONObject jsonObject = null;
        try {
            String jsonStr = readAssetsFile(context, filePath);
            jsonObject = new JSONObject(jsonStr);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jsonObject;
    }

    static byte[] Decrypt(byte[] src) {
        for (int n = 0; n < src.length; n++) {
            src[n] = (byte) (src[n] ^ 359);
        }
        return src;
    }

    public static String readFile(String filePath) {
        return null;
    }

    public static String getChannelName(Context context) {
        String channelName = "";
        ApplicationInfo info = context.getApplicationInfo();
        String sourceDir = info.sourceDir;
        ZipFile zipFile = null;
        try {
            zipFile = new ZipFile(sourceDir);
            Enumeration<?> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                String entryName = entry.getName();
                int index = entryName.indexOf(CHANNEL_PREFIX);
                if (entryName.startsWith(CHANNEL_PREFIX) && entryName.length() > index) {
                    channelName = entryName.replace(CHANNEL_PREFIX, "");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return channelName;
    }

    /**
     *
     * @param context
     * @param sourcePath
     * @param destPath
     */
    public static void copyFileFromAsset(Context context, String sourcePath, String destPath) {
        int length;
        byte[] buff=new byte[1024];
        InputStream ips ;
        FileOutputStream fos;
        try {
            ips = context.getAssets().open(sourcePath);
            fos = new FileOutputStream(destPath);
            while ((length = ips.read(buff))!=-1){
                fos.write(buff,0,length);
            }
            fos.flush();
            fos.close();
            ips.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copyFile(String sourcePath, String destPath){
        int length;
        byte[] buff=new byte[1024];
        InputStream ips ;
        FileOutputStream fos;
        try {
            ips = new FileInputStream(new File(sourcePath));
            fos = new FileOutputStream(destPath);
            while ((length = ips.read(buff))!=-1){
                fos.write(buff,0,length);
            }
            fos.flush();
            fos.close();
            ips.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
